from typing import List, Optional
from uuid import UUID
from fastapi import APIRouter, Depends, status
from sqlalchemy.ext.asyncio import AsyncSession

from db.session import get_db
from schemas.remediations import (
    Remediation, 
    RemediationCreate, 
    RemediationUpdate, 
    RemediationDeleteRequest,
    RemediationStatusUpdate,
    RiskLevel,
    RemediationStatus
)
from core.remediations import (
    get_remediation_items_by_plan,
    create_new_remediation_item,
    update_remediation_item as core_update_remediation_item,
    update_remediation_item_status as core_update_remediation_item_status,
    delete_remediation_item as core_delete_remediation_item,
    delete_multiple_remediation_items
)
from log import log_info, log_error

router = APIRouter(prefix="/remediation", tags=["remediation"])

@router.get("/plan/{plan_id}", response_model=List[Remediation])
async def read_remediations_by_plan(
    plan_id: UUID,
    skip: int = 0,
    limit: int = 100,
    status: Optional[RemediationStatus] = None,
    risk_level: Optional[RiskLevel] = None,
    db: AsyncSession = Depends(get_db)
):
    """
    获取特定评估计划的所有整改项
    
    Args:
        plan_id: 评估计划ID
        skip: 跳过的记录数
        limit: 返回的最大记录数
        status: 按状态过滤
        risk_level: 按风险等级过滤
        db: 数据库会话
        
    Returns:
        整改项列表及总数
    """
    items = await get_remediation_items_by_plan(
        db=db, 
        plan_id=plan_id, 
        skip=skip, 
        limit=limit,
        status=status,
        risk_level=risk_level
    )
    
    return items

@router.post("/", response_model=Remediation, status_code=status.HTTP_201_CREATED)
async def create_new_remediation(
    remediation: RemediationCreate, 
    db: AsyncSession = Depends(get_db)
):
    """
    创建新的整改项
    
    Args:
        remediation: 整改项创建模型
        db: 数据库会话
        
    Returns:
        创建的整改项
    """
    return await create_new_remediation_item(db=db, remediation=remediation)

@router.put("/{remediation_id}", response_model=Remediation)
async def update_remediation_item(
    remediation_id: UUID, 
    remediation: RemediationUpdate, 
    db: AsyncSession = Depends(get_db)
):
    """
    更新现有整改项
    
    Args:
        remediation_id: 整改项ID
        remediation: 整改项更新模型
        db: 数据库会话
        
    Returns:
        更新后的整改项
    """
    return await core_update_remediation_item(db=db, remediation_id=remediation_id, remediation=remediation)

@router.patch("/{remediation_id}/status", response_model=Remediation)
async def update_remediation_item_status(
    remediation_id: UUID, 
    status_update: RemediationStatusUpdate, 
    db: AsyncSession = Depends(get_db)
):
    """
    更新整改项状态
    
    Args:
        remediation_id: 整改项ID
        status_update: 状态更新模型
        db: 数据库会话
        
    Returns:
        更新后的整改项
    """
    return await core_update_remediation_item_status(db=db, remediation_id=remediation_id, status_update=status_update)

@router.delete("/{remediation_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_remediation_item(
    remediation_id: UUID, 
    db: AsyncSession = Depends(get_db)
):
    """
    删除整改项
    
    Args:
        remediation_id: 整改项ID
        db: 数据库会话
    """
    await core_delete_remediation_item(db=db, remediation_id=remediation_id)
    return None

@router.delete("/", status_code=status.HTTP_204_NO_CONTENT)
async def delete_multiple_remediations(
    payload: RemediationDeleteRequest, 
    db: AsyncSession = Depends(get_db)
):
    """
    批量删除整改项
    
    Args:
        payload: 包含要删除的整改项ID列表的请求体
        db: 数据库会话
    """
    await delete_multiple_remediation_items(db=db, remediation_ids=payload.ids)
    return None